﻿using System;
using ZFine.Application.Gas;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.Entity.SystemManage;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace ZFine.Web.Areas.Gas.Controllers
{
    public class StatisticsController : ControllerBase
    {
        private ResidentApp residentApp = new ResidentApp();
        private FaceRecordApp faceRecordApp = new FaceRecordApp();

        [HttpGet]
        [HandlerAuthorize(ignore:false)]
        public override ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Form()
        {
            return View();
        }
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Details()
        {
            return View();
        }
        [HandlerAuthorize(ignore: false)]
        public ActionResult MonthList()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public ActionResult Month()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public ActionResult Quarter()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public ActionResult Year()
        {
            return View();
        }
        [HandlerAuthorize(ignore: false)]
        public ActionResult YearList()
        {
            return View();
        }
        /// <summary>
        /// 月度统计 列表
        /// </summary>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonMonth( string Month)
        {
            int year = 0;
            int month = 0;
            if (!string.IsNullOrEmpty(Month)&&Month.Length==8)
            {
                int.TryParse(Month.Substring(0,4), out year);
                int.TryParse(Month.Substring(5, 2), out month);
            }
            List<FaceRecordEntity> l_faceRecord = faceRecordApp.GetMonthList( year, month).Where(a=>a.Result== ZFine.Application.Enum.Gas.FaceRecordResult.已刷卡.ToString()).ToList();
            var rows = from _faceRecord in l_faceRecord
                       group _faceRecord by new {_faceRecord.Year,_faceRecord.Month }
                       into s
                       select new
                       {
                           Year = s.Select(a => a.Year).First(),
                           Month = s.Select(a => a.Month).First(),
                           SubsidyCountMonth = s.Count(),
                           SubsidyAmountMonth= s.Count()*3,
                       };
            return Content(rows.ToJson());
        }
        /// <summary>
        /// 年度统计 列表
        /// </summary>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonYear()
        {
            List<FaceRecordEntity> l_faceRecord = faceRecordApp.GetMonthList(0, 0).Where(a => a.Result == ZFine.Application.Enum.Gas.FaceRecordResult.已刷卡.ToString()).ToList();
            var rows = from _faceRecord in l_faceRecord
                       group _faceRecord by new { _faceRecord.Year}
                       into s
                       select new
                       {
                           Year = s.Select(a => a.Year).First(),
                           SubsidyCountMonth = s.Count(),
                           SubsidyAmountMonth = s.Count() * 3,
                       };
            return Content(rows.ToJson());
        }
        /// <summary>
        /// 月度统计 当月明细
        /// </summary>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonMonthDetails(Pagination pagination, ResidentEntity in_resident,string Year,string Month,string txtResult)
        {
            List<FaceRecordEntity> l_faceRecord = faceRecordApp.GetPageMonthList(pagination, in_resident.RName, in_resident.IDNumber, Year,Month, txtResult);
            var rows = l_faceRecord;
            var data = new
            {
                page = pagination.page,//当前页  1  2 3 之类
                records = pagination.records,//总条数  这里应该是18
                rows = rows,//分页的数据
                total = pagination.total,//页数
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, ResidentEntity in_resident)
        {
            List<ResidentEntity> l_resident = residentApp.GetList(pagination, in_resident.RName,in_resident.IDNumber,"","");
            //因为有部分车牌没有记录，需要使用左连接
            var rows = from _resident in l_resident
                       select new
                       {
                           _resident.F_Id,
                           _resident.RName,
                           _resident.IDNumber,
                           FaceRecognizeTime=_resident.FaceRecognizeTime.ToString(PubClass.TimeFormat)
                       };

            var data = new
            {
                page = pagination.page,//当前页  1  2 3 之类
                records = pagination.records,//总条数  这里应该是18
                rows = rows,//分页的数据
                total = pagination.total,//页数
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = residentApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(ResidentEntity residentEntity, string permissionIds, string keyValue)
        {
            //只修改，不新增
            residentApp.SubmitForm(residentEntity, keyValue);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize(ignore:false)]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            residentApp.Del(keyValue);
            return Success("删除成功。");
        }
    }
}
